package com.telit.dispose.controller.resourceManagement;


import com.telit.common.constant.Constant;
import com.telit.common.entity.DataReturn;
import com.telit.common.entity.PageData;
import com.telit.common.enums.ResultCode;
import com.telit.dispose.dto.FireResourceCategoryDTO;
import com.telit.dispose.entity.FireResourceCategory;
import com.telit.dispose.model.QryDeviceListRequest;
import com.telit.dispose.model.QryDeviceModel;
import com.telit.dispose.model.QryResourceListRequest;
import com.telit.dispose.model.QryResourceModel;
import com.telit.dispose.resource.QryCatListRequest;
import com.telit.dispose.service.resourceManagement.FireResourceService;
import com.telit.dispose.vo.resourceManagement.FireSituationVO;
import com.telit.dispose.vo.resourceManagement.ResourceCategoryVO;
import com.telit.dispose.vo.resourceManagement.ResourceSimpVO;
import com.telit.dispose.vo.resourceManagement.TopFloorDownDistrictCodeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * Create by Chencf 2022/09/05
 */
@Slf4j
@RestController
@RequestMapping(value = "/fire-resource", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = "防火资源API", value = "防火资源API", produces = MediaType.APPLICATION_JSON_VALUE)
public class FireResourceController {

    @Autowired
    private FireResourceService fireResourceService;

    //新增防火资源
    @PostMapping(value = "/addResource", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "01--新增防火资源")
    public DataReturn addResource(@RequestBody FireResourceCategory fireResourceCategory) {
        fireResourceCategory.setCreatedAt(new Date());
        fireResourceCategory.setUpdatedAt(new Date());
        fireResourceService.addResource(fireResourceCategory);
        return DataReturn.ok();
    }

    /**
     * 查询分类树
     *
     * @return DataReturn Obj
     */
    @ApiOperation(value = "02--查询分类树")
    @GetMapping(path = "/qry-cat-tree")
    public DataReturn<List<FireResourceCategoryDTO>> qryCatTree() {
        return DataReturn.ok(fireResourceService.qryCatTree());
    }

    /**
     * 查询分类列表
     *
     * @param qryCatListRequest QryCatListRequest
     *
     * @return DataReturn Obj
     */
    @ApiOperation(value = "03--查询分类列表")
    @GetMapping(path = "/qry-cat-list")
    public DataReturn<List<FireResourceCategoryDTO>> qryCatList(QryCatListRequest qryCatListRequest) {
        return DataReturn.ok(fireResourceService.qryCatList(qryCatListRequest));
    }

    @ApiOperation("04--根据父节点id查询(不包含子类的简要数据)")
    @GetMapping(path = "/topFloorList")
    public DataReturn<List<ResourceCategoryVO>> topFloorList(
            @RequestParam(value = "parentId", required = false) Long parentId) {
        return DataReturn.ok(fireResourceService.topFloorList(parentId));
    }

    //查询所有的顶层分类下的资源列表
    @ApiOperation("05--根据范围查询查询所有顶层资源类别（包含子类及子类数量）parentId=0 ")
    @GetMapping(path = "/topFloorDownChildrenList")
    public DataReturn<List<ResourceCategoryVO>> topFloorDownChildrenList(
            @RequestParam(value = "parentId", required = false) Long parentId,
            @RequestParam(value = "longitude", required = false) String longitude,
            @RequestParam(value = "latitude", required = false) String latitude,
            @RequestParam(value = "radius", required = false) String radius,
            @RequestParam(value = "districtCode", required = false) String districtCode
    ) {
        return DataReturn.ok(
                fireResourceService.topFloorDownChildrenList(parentId, longitude, latitude, radius, districtCode));
    }

    @Deprecated
//    @ApiOperation("08--查询其他资源数据(系统固有资源) ")
//    @GetMapping(path = "/findOtherResource")
    public DataReturn<List<ResourceCategoryVO>> findOtherResource(
            @RequestParam(value = "parentId", required = false) Long parentId,
            @RequestParam(value = "longitude", required = false) String longitude,
            @RequestParam(value = "latitude", required = false) String latitude,
            @RequestParam(value = "radius", required = false) String radius) {
        return DataReturn.ok(fireResourceService.findOtherResource(parentId, longitude, latitude, radius));
    }


    @ApiOperation("06--根据tableId tableName 查询资源表简要数据")
    @GetMapping(path = "/findTableSimpInfo")
    public DataReturn<List<ResourceSimpVO>> findTableSimpInfo(
            @RequestParam(value = "tableName", required = true) String tableName,
            @RequestParam(value = "tableId", required = true) String tableId,
            @RequestParam(value = "longitude", required = false) String longitude,
            @RequestParam(value = "latitude", required = false) String latitude,
            @RequestParam(value = "radius", required = false) String radius,
            @RequestParam(value = "districtCode", required = false) String districtCode
    ) {
        return DataReturn.ok(
                fireResourceService.findTableSimpInfo(tableName, tableId, longitude, latitude, radius, districtCode));
    }

    //修改资源,目前只能支持修改image
    @PostMapping(value = "/updateResource", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "07--修改资源,目前只支持修改image")
    public DataReturn updateResource(@RequestBody FireResourceCategory fireResourceCategory) {
        fireResourceService.updateResource(fireResourceCategory);
        return DataReturn.ok();
    }


    @ApiOperation("08--根据范围查询查询所有顶层资源类别所有的行政区划树parentId=0 ")
    @GetMapping(path = "/topFloorDownDistrictCodeChildrenList")
    public DataReturn<List<TopFloorDownDistrictCodeVO>> topFloorDownDistrictCodeChildrenList(
            @RequestParam(value = "parentId", required = false) Long parentId,
            @RequestParam(value = "longitude", required = false) String longitude,
            @RequestParam(value = "latitude", required = false) String latitude,
            @RequestParam(value = "radius", required = false) String radius,
            @RequestParam(value = "districtCode", required = false) String districtCode
    ) {
        return DataReturn.ok(
                fireResourceService.topFloorDownDistrictCodeChildrenList(parentId, longitude, latitude, radius,
                        districtCode));
    }


    @ApiOperation(value = "01-----火场态势周围资源查询-需要字段返回 flag表示资源所属的分类 1机构人员  2 基础设施  3 阻隔系统  4 其他设施 ")
    @GetMapping(value = "/getSimpleGeoSourceNearbyRadius", produces = "application/json;charset=UTF-8")
    public DataReturn getSimpleGeoSourceNearbyRadius(
            @RequestParam(value = "longitude", required = false) String longitude,
            @RequestParam(value = "latitude", required = false) String latitude,
            @RequestParam(value = "radius", required = false) String radius,
            @RequestParam(value = "flag", required = false) String flag,
            @RequestParam(value = "districtCode", required = false) String districtCode,
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "rows", required = false) Integer rows
    ) {
        try {
            Map<String, List<FireSituationVO>> geoSourceNearbyRadiusMap = fireResourceService.getSimpleGeoSourceNearbyRadius(
                    longitude, latitude, radius, flag, districtCode, page, rows);
            return DataReturn.ok(geoSourceNearbyRadiusMap);
        } catch (Exception e) {
//            logger.error("获取火场态势周围资源个别字段失败,e:{}",e.getMessage());
            return DataReturn.failure(ResultCode.SERVER_ERROR, "查询火场态势周围资源个别失败");
        }
    }


    @ApiOperation("06--根据tableId tableName 查询资源表简要数据")
    @GetMapping(path = "/findTableSimpInfoU")
    public DataReturn<List<ResourceSimpVO>> findTableSimpInfoU(
            @RequestParam(value = "tableName", required = true) String tableName,
            @RequestParam(value = "tableId", required = true) String tableId,
            @RequestParam(value = "longitude", required = false) String longitude,
            @RequestParam(value = "latitude", required = false) String latitude,
            @RequestParam(value = "radius", required = false) String radius,
            @RequestParam(value = "districtCode", required = false) String districtCode,
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "rows", required = false) Integer rows
    ) {
        return DataReturn.ok(
                fireResourceService.findTableSimpInfoU(tableName, tableId, longitude, latitude, radius, districtCode,
                        page, rows));
    }

    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "查询资源列表（返回指定公共字段）")
    @GetMapping(path = "/qry-resource-list")
    public DataReturn<PageData<List<QryResourceModel>>> qryResourceList(QryResourceListRequest qryResourceListRequest) {
        return DataReturn.ok(fireResourceService.qryResourceList(qryResourceListRequest));
    }

    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "查询设备列表（返回指定公共字段）")
    @GetMapping(path = "/qry-device-list")
    public DataReturn<PageData<List<QryDeviceModel>>> qryDeviceList(QryDeviceListRequest qryDeviceListRequest) {
        return DataReturn.ok(fireResourceService.qryDeviceList(qryDeviceListRequest));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableId", value = "表id", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "String"),
    })
    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "设施详情")
    @GetMapping(path = "/qry-device-detail/{tableId}/{id}")
    public DataReturn<QryDeviceModel> qryDeviceDetail(@PathVariable(value = "tableId") String tableId,
                                                      @PathVariable(value = "id") String id) {
        return DataReturn.ok(fireResourceService.qryDeviceDetail(tableId, id));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableId", value = "表id", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "String"),
    })
    @ApiOperation(httpMethod = Constant.REQUEST_METHOD_GET, value = "资源详情")
    @GetMapping(path = "/qry-resource-detail/{tableId}/{id}")
    public DataReturn<QryResourceModel> qryResourceDetail(@PathVariable(value = "tableId") String tableId,
                                                          @PathVariable(value = "id") String id) {
        return DataReturn.ok(fireResourceService.qryResourceDetail(tableId, id));
    }

}
